{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "#### Runnable协议\n",
    "    Runnable定义的公共的调用方法如下：\n",
    "        invoke：处理单条输入，等待LLM完全推理完成后再返回调用结果\n",
    "        stream：流式响应，逐字输出LLM的响应结果\n",
    "        batch：处理批量输入\n",
    "    相应的异步方法：\n",
    "        astream：异步流式响应\n",
    "        ainvoke：异步处理单条输入\n",
    "        abatch：异步处理批量输入\n",
    "        astream_log：异步流式返回中间步骤，以及最终响应\n",
    "        astream_events：(测试版)异步流式返回链中发生的事件"
   ],
   "id": "2f060a987fa35ba4"
  },
  {
   "metadata": {
    "SqlCellData": {
     "variableName$1": "df_sql1"
    }
   },
   "cell_type": "markdown",
   "source": "#### 流式响应",
   "id": "515bf7f8e17301e8"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.messages import SystemMessage, HumanMessage\n",
    "import os\n",
    "\n",
    "os.environ['OPENAI_BASE_URL'] = 'https://vip.apiyi.com/v1'\n",
    "os.environ['OPENAI_API_KEY'] = 'sk-xU64G4hXJ4L47ko3764958119dB245D2BdEcE528767dA1Da'\n",
    "\n",
    "chat_model = ChatOpenAI(model=\"gpt-4o-mini\", streaming=True)\n",
    "\n",
    "system_message = SystemMessage(content=\"我是人工只能助手，我叫小智\")\n",
    "human_message = HumanMessage(content=\"猫王是一只猫吗?\")\n",
    "\n",
    "messages = [system_message, human_message]\n",
    "\n",
    "for chunk in chat_model.stream(messages):\n",
    "    print(chunk.content, end=\"\", flush=True)"
   ],
   "id": "e4c263a9744066de",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 批量调用",
   "id": "2c5c3b7ae0033c32"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.messages import SystemMessage, HumanMessage\n",
    "import os\n",
    "\n",
    "os.environ['OPENAI_BASE_URL'] = 'https://vip.apiyi.com/v1'\n",
    "os.environ['OPENAI_API_KEY'] = 'sk-xU64G4hXJ4L47ko3764958119dB245D2BdEcE528767dA1Da'\n",
    "\n",
    "chat_model = ChatOpenAI(model=\"gpt-4o-mini\")\n",
    "\n",
    "system_message = SystemMessage(content=\"我是人工只能助手，我叫小智\")\n",
    "human_message = HumanMessage(content=\"猫王是一只猫吗?\")\n",
    "\n",
    "messages1 = [system_message, human_message]\n",
    "messages2 = [system_message, human_message]\n",
    "\n",
    "messages = [messages1, messages2]\n",
    "\n",
    "response = chat_model.batch(messages)\n",
    "print(response)"
   ],
   "id": "73fba2b1ed71b9a0",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
